package neusoft.edu.cn.a215lmeordering.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import neusoft.edu.cn.a215lmeordering.Listener.Listener;
import neusoft.edu.cn.a215lmeordering.R;
import neusoft.edu.cn.a215lmeordering.bean.Login;
import neusoft.edu.cn.a215lmeordering.model.LoginModel;


public class LoginActivity extends AppCompatActivity {

    Button register;//注册
    Button login;//登录
    TextView verification_code_login;
    EditText userName;
    EditText passWord;

    private String username = "", password = "";
    public static String UserId;

    private Listener loginListener = new Listener() {
        @Override
        public void onResponse(Object o) {
            Login login = (Login) o;
            UserId = login.getuserid();
            if(UserId.equals("0")){
                Toast.makeText(LoginActivity.this, "登陆失败了", Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(LoginActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
                final Intent intent_login = new Intent(LoginActivity.this,MainActivity.class);
                intent_login.putExtra("user_id",Integer.parseInt(login.getuserid()));
                System.out.println("user_id:"+login.getuserid());
                startActivity(intent_login);
                finish();
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(getApplicationContext(), "失败："+msg, Toast.LENGTH_SHORT).show();
        }


    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActionBar actionBar = getSupportActionBar();//去掉标题栏
        actionBar.hide();//去掉标题栏
        setContentView(R.layout.activity_login);

        init();

        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final Intent intent_register = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent_register);
//                finish();
            }
        });

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                username = userName.getText().toString();
                password = passWord.getText().toString();

                if(username.equals(""))
                {
                    Toast.makeText(LoginActivity.this, "请输入账号", Toast.LENGTH_SHORT).show();
                }else if(password.equals("")){
                    Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                }else{
                    LoginModel loginModel = new LoginModel();
                    loginModel.getResultList(username,password,loginListener);
                }

            }
        });


        verification_code_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final Intent intent_code = new Intent(LoginActivity.this,Login_phone_Activity.class);
                startActivity(intent_code);
                finish();
            }
        });

    }

    private void init() {
        register = findViewById(R.id.button3);//注册
        login = findViewById(R.id.button);//登录
        verification_code_login = findViewById(R.id.verification_code_login);
        userName = findViewById(R.id.username);
        passWord = findViewById(R.id.password);
    }
}
